package cmd

import (
	"os"

	log "github.com/sirupsen/logrus"

	"github.com/urfave/cli/v2"
)

type AppInfo struct {
	Version string
}

func init() {
	log.SetOutput(os.Stdout)
	log.SetLevel(log.DebugLevel)
	log.SetFormatter(&log.TextFormatter{
		ForceColors:      true,
		DisableTimestamp: true,
	})
}

func AppInit(a *AppInfo) {

	app := &cli.App{}

	app.Name = "siphan"
	app.Version = a.Version
	app.Usage = "A safe tool to encrypt and decrypt data"

	app.Commands = []*cli.Command{
		{
			Name:    "encode",
			Aliases: []string{"e"},
			Usage:   "to encrypt data",
			Action:  EncodeHandler,
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:     "file",
					Required: true,
					Aliases:  []string{"f"},
					Usage:    "define the filename of file data",
				},
				&cli.StringFlag{
					Name:    "key",
					Aliases: []string{"k"},
					Usage:   "define the filename of key map",
				},
			},
		},
		{
			Name:    "decode",
			Aliases: []string{"d"},
			Usage:   "to decrypt data",
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:     "file",
					Required: true,
					Aliases:  []string{"f"},
					Usage:    "define the filename of file data",
				},
				&cli.StringFlag{
					Name:     "key",
					Required: true,
					Aliases:  []string{"k"},
					Usage:    "define the filename of key map",
				},
				&cli.StringFlag{
					Name:     "output",
					Required: true,
					Aliases:  []string{"o"},
					Usage:    "provide the file name of the decrypt result",
				},
			},
			Action: DecodeHandler,
		},
	}

	app.Action = func(c *cli.Context) error {
		return nil
	}

	if err := app.Run(os.Args); err != nil {
		log.Errorln(err)
	}
}
